<template>
  <div class="mode-selection">
    <div class="page-header">
      <button class="back-btn" @click="handleGoBack">
        <span class="back-icon">←</span>
        返回
      </button>
      <h1 class="page-title">🎯 选择学习模式</h1>
      
      <!-- 加载状态 -->
      <div v-if="subCategoryLoading" class="loading-subtitle">
        <div class="loading-spinner-small"></div>
        正在加载学习方向信息...
      </div>
      
      <!-- 错误状态 -->
      <div v-else-if="subCategoryError" class="error-subtitle">
        ❌ {{ subCategoryError }}
        <button class="retry-btn-small" @click="fetchSubCategoryData">重试</button>
      </div>
      
      <!-- 正常显示 -->
      <p v-else-if="subCategory" class="page-subtitle">学习方向：{{ subCategory.name }}</p>
      
      <!-- 默认状态 -->
      <p v-else class="page-subtitle">学习方向：加载中...</p>
    </div>

    <!-- 学习模式选择 -->
    <div class="learning-modes-section">
      <div class="section-header">
        <h2>📚 学习方式</h2>
        <p>选择适合你的学习方式</p>
      </div>
      
      <!-- 数据未加载完成时禁用按钮 -->
      <div class="learning-modes-grid">
        <div 
          class="learning-mode-card" 
          :class="{ 'disabled': !subCategory || subCategoryLoading }"
          @click="subCategory && !subCategoryLoading ? selectKnowledgeLearning() : null"
        >
          <div class="mode-icon">📖</div>
          <h3 class="mode-title">知识学习</h3>
          <p class="mode-description">系统化学习知识点，无需答题，专注于知识掌握</p>
          <div class="mode-features">
            <span class="feature-tag">系统学习</span>
            <span class="feature-tag">知识梳理</span>
            <span class="feature-tag">无需答题</span>
          </div>
        </div>
        <div 
          class="learning-mode-card" 
          :class="{ 'disabled': !subCategory || subCategoryLoading }"
          @click="subCategory && !subCategoryLoading ? selectQuestionMode() : null"
        >
          <div class="mode-icon">✍️</div>
          <h3 class="mode-title">去答题</h3>
          <p class="mode-description">通过答题练习巩固知识，检验学习效果</p>
          <div class="mode-features">
            <span class="feature-tag">答题练习</span>
            <span class="feature-tag">即时反馈</span>
            <span class="feature-tag">巩固知识</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCategoryDetail } from '@/api/categories'

export default {
  name: "ModeSelection",
  props: {
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      category: this.$route.params.category,
      subCategoryId: this.$route.params.subCategoryId,
      subCategory: null,
      subCategoryLoading: false,
      subCategoryError: null,
      navigationInProgress: false
    }
  },
  async mounted() {
    await this.fetchSubCategoryData();
  },
  methods: {
    async fetchSubCategoryData() {
      try {
        this.subCategoryLoading = true;
        this.subCategoryError = null;
        
        const response = await getCategoryDetail(this.subCategoryId);
        if (response && response.data) {
          this.subCategory = {
            ...response.data,
            categoryId: this.category?.id
          };
        } else {
          throw new Error('获取子分类数据失败');
        }
      } catch (error) {
        console.error('获取子分类数据失败:', error);
        this.subCategoryError = error.message || '获取子分类数据失败，请稍后重试';
        this.subCategory = null;
      } finally {
        this.subCategoryLoading = false;
      }
    },
    
    selectKnowledgeLearning() {
      if (!this.subCategory || this.navigationInProgress) {
        console.error('子分类数据未加载完成或导航进行中');
        return;
      }
      
      this.navigationInProgress = true;
      
      this.$emit('select-mode', {
        mode: 'knowledge',
        category: this.category,
        subCategory: this.subCategory
      });
      
      // 延迟重置导航标志
      setTimeout(() => {
        this.navigationInProgress = false;
      }, 1000);
    },

    selectQuestionMode() {
      if (!this.subCategory || this.navigationInProgress) {
        console.error('子分类数据未加载完成或导航进行中');
        return;
      }
      
      this.navigationInProgress = true;
      
      this.$emit('select-mode', {
        mode: 'question',
        category: this.category,
        subCategory: this.subCategory
      });
      
      // 延迟重置导航标志
      setTimeout(() => {
        this.navigationInProgress = false;
      }, 1000);
    },

    handleGoBack() {
      this.$router.push({ name: 'learn-category' });
    }
  }
}
</script>

<style scoped>
/* 学习方式选择页面 */
.mode-selection {
  max-width: 1000px;
  margin: 0 auto;
}

/* 页面头部样式 */
.page-header {
  text-align: center;
  margin-bottom: 40px;
  padding: 30px 0;
}

.page-title {
  font-size: 2rem;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 10px 0;
}

.page-subtitle {
  font-size: 1rem;
  color: #7f8c8d;
  margin: 0;
}

/* 加载状态样式 */
.loading-subtitle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: #7f8c8d;
  font-size: 0.9rem;
  margin-top: 10px;
}

.loading-spinner-small {
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3b82f6;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 错误状态样式 */
.error-subtitle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: #e74c3c;
  font-size: 0.9rem;
  margin-top: 10px;
}

.retry-btn-small {
  padding: 8px 12px;
  border: 1px solid #e74c3c;
  border-radius: 15px;
  font-size: 0.8rem;
  color: #e74c3c;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 600;
}

.retry-btn-small:hover {
  background: #e74c3c;
  color: white;
  border-color: #e74c3c;
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(231, 76, 60, 0.2);
}

/* 学习模式选择区域 */
.learning-modes-section {
  margin-bottom: 40px;
}

.section-header {
  text-align: center;
  margin-bottom: 30px;
}

.section-header h2 {
  font-size: 1.5rem;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 10px 0;
}

.section-header p {
  font-size: 1rem;
  color: #7f8c8d;
  margin: 0;
}

.learning-modes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 30px;
  margin-top: 30px;
}

.learning-mode-card {
  background: white;
  border-radius: 25px;
  padding: 40px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  border: 2px solid transparent;
  transition: all 0.3s ease;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.learning-mode-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  border-color: #3b82f6;
}

.learning-mode-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.learning-mode-card:hover::before {
  transform: scaleX(1);
}

.mode-icon {
  font-size: 4rem;
  margin-bottom: 20px;
  display: block;
}

.mode-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 15px 0;
}

.mode-description {
  font-size: 1.1rem;
  color: #7f8c8d;
  line-height: 1.6;
  margin: 0 0 25px 0;
}

.mode-features {
  display: flex;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

.feature-tag {
  padding: 8px 16px;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1) 0%, rgba(59, 130, 246, 0.05) 100%);
  color: #3b82f6;
  border-radius: 20px;
  font-size: 0.9rem;
  font-weight: 600;
  border: 1px solid rgba(59, 130, 246, 0.2);
}

.back-btn {
  padding: 12px 24px;
  border: 2px solid #e9ecef;
  border-radius: 25px;
  font-size: 1rem;
  color: #3b82f6;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.back-btn:hover {
  background: #3b82f6;
  color: white;
  border-color: #3b82f6;
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(59, 130, 246, 0.3);
}

.back-icon {
  font-size: 1.1rem;
}

/* 禁用状态样式 */
.learning-mode-card.disabled {
  opacity: 0.7;
  cursor: not-allowed;
  pointer-events: none;
  background: #f0f0f0;
  border-color: #e0e0e0;
  box-shadow: none;
  transform: none;
}

.learning-mode-card.disabled .mode-icon {
  color: #b0bec5;
}

.learning-mode-card.disabled .mode-title {
  color: #95a5a6;
}

.learning-mode-card.disabled .mode-description {
  color: #bdc3c7;
}

.learning-mode-card.disabled .feature-tag {
  background: #e0e0e0;
  color: #95a5a6;
  border: 1px solid #d0d0d0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-title {
    font-size: 1.6rem;
  }

  .page-subtitle {
    font-size: 0.9rem;
  }

  .learning-modes-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }

  .learning-mode-card {
    padding: 30px 20px;
  }

  .mode-icon {
    font-size: 3rem;
  }

  .mode-title {
    font-size: 1.3rem;
  }

  .mode-description {
    font-size: 1rem;
  }
}

@media (max-width: 480px) {
  .page-title {
    font-size: 1.4rem;
  }

  .page-subtitle {
    font-size: 0.8rem;
  }

  .learning-mode-card {
    padding: 25px 15px;
  }

  .mode-icon {
    font-size: 2.5rem;
  }

  .mode-title {
    font-size: 1.2rem;
  }

  .mode-description {
    font-size: 0.9rem;
  }

  .feature-tag {
    padding: 6px 12px;
    font-size: 0.8rem;
  }
}
</style>
